const { Service } = require('egg');
const { v4: uuidV4 } = require('uuid');
const moment = require('moment');
const notice_table = 'notice', user_table = 'user', notice_read_table = 'notice_read'

class NoticeService extends Service{

  async list(title, type, content, order, method, pageIndex, pageSize) {
    const ctx = this.ctx;
    try {
      const sql = ctx.knex().select(knex.raw(`t1.uuid, t1.title, t1.content, t1.update_time, t1.update_by,
        t1.create_time, t1.create_by, t2.name as create_name, t3.name as update_name, if(t4.uuid = null, 0, 1) as is_read`)
      )
      .from(`${notice_table} as t1`)
      .leftJoin(`${user_table} as t2`, () => {
        this.on('t2.create_by', '=', 't1.uuid')
      })

      const countSql = ctx.knex().count('t1.uuid as count').from(notice_table)

      if(title) {
        sql.andWhereLike('t1.title', `%${title}%`);
        countSql.andWhereLike('title', `%${title}%`);
      }

      if(type) {
        sql.andWhereLike('t1.title', `%${type}%`)
        countSql.andWhereLike('title', `%${type}%`)
      }

      if(content) {
        sql.andWhereLike('t1.content', `%${content}%`)
        countSql.andWhereLike('content', `%${content}%`)
      }

      const list = await sql.orderBy(order, method).limit(Number(pageSize)).offset((Number(pageIndex) - 1) * Number(pageSize))

      const count = await countSql;

      return {
        list,
        count: count[0].count
      }
    } catch (error) {
      throw error
    }
  }

  async info(id) {
    const ctx = this.ctx;
    try {
      const info = await ctx.knex().select('t1.uuid as id', 't1.title', 't1.content', 't1.create_time', 't1.create_by',
        't1.update_by', 't1.update_time', 't2.name as create_name', 't3.name as update_name'
      ).from(`${notice_table} as t1`)
      .leftJoin(`${user_table} as t2`, () => {
        this.on('t1.create_by', '=', 't2.uuid')
      })
      .leftJoin(`${user_table} as t3`, () => {
        this.on('t1.update_by', '=', 't3.uuid')
      })
      .where({uuid: id});


      return info;
    } catch (error) {
      throw error
    }
  }

  async insert(title, content) {
    const ctx = this.ctx;
    try {
      const { id } = ctx.session;

      // 确定当前消息发送的主题
      const notice_instance = {
        uuid: uuidV4(),
        title,
        content,
        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        update_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        create_by: id
      }

      await ctx.knex(notice_table).insert(notice_instance);

      return;
    } catch (error) {
      throw error;
    }
  }

  async update(notice_ids) {
    const ctx = this.ctx;
    try {
      const { id } = ctx.session;

      const notice_read_instance = [];

      for (const notice_id of notice_ids) {
        const obj = {
          uuid: uuidV4(),
          notice_id,
          user_id: id
        };

        notice_read_instance.push(obj);
      }

      await ctx.knex(notice_read_table).insert(notice_read_instance);

      return;
    } catch (error) {
      throw error
    }
  }

  async readCount() {
    const ctx = this.ctx;
    try {
      const { id } = ctx.session
      const data = await ctx.knex(`${notice_table} as t1`)
      .count('t1.uuid')
      .leftJoin(`${notice_read_table} as t2`, function() {
        this.on('t1.uuid', '=', 't2.notice_id')
      })
      .where('t2.user_id', '!=', id);

      return data[0].uuid
    } catch (error) {
      throw error
    }
  }

  // async product(topic, content, ) {
  //   const ctx = this.ctx;
  //   try {
  //     const channel = await this.app.rabbit.createChannel();

  //   } catch (error) {
  //     throw error;
  //   }
  // }

}

module.exports = NoticeService